import { createApp } from "vue";
import { createPinia } from "pinia";
import ElementPlus, { ElMessage } from "element-plus";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
import ECharts from "vue-echarts";
import "echarts";

// @ts-ignore
import Vue3videoPlay from "vue3-video-play";
import "vue3-video-play/dist/style.css"; // 引入css
import "./index.css";
import App from "./App.vue";
import router from "./router.ts";
import "element-plus/dist/index.css";
import useUserStore from "./store/userStore.ts";
import "./style.css";
import useSectionStore from "./store/sectionStore.ts";
// import VuePdf from "vue3-pdfjs";



const app = createApp(App);

const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

app.use(router);
app.use(pinia);
app.use(ElementPlus);
app.use(Vue3videoPlay);
app.component("v-chart", ECharts)


// app.use(VuePdf);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

const userStore = useUserStore();
const sectionStore = useSectionStore();

router.beforeEach((to) => {
  if (to.meta.isNeedLogin) {
    if (userStore.user === null) {
      ElMessage({
        message: "请先登录",
      });
      sectionStore.setRouteURL(to.path);
      return { name: "login" };
    }
    if (to.meta.isNeedAdmin && !userStore.isAdmin) {
      ElMessage({
        message: "请先登录管理员",
      });
      sectionStore.setRouteURL(to.path);
      return { name: "login" };
    }
  }
  return true;
});

app.mount("#app");
